package com.reformer.commons.utils;

import com.reformer.commons.constant.ThirdPayType;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import java.util.UUID;

public class GenerateUtil {

    private GenerateUtil() {
    }

    /**
     * 产生22位的原始订单号（仅包含数字），开发生可根据情况自行设置
     *
     * @param date            进场时间
     * @param parkingrecordid 停车记录id
     * @return
     */
    private static String genOutTradeNo(Date date, String parkingrecordid) {
        if (date == null) {
            date = new Date();
        }
        if (parkingrecordid == null || parkingrecordid.length() == 0) {
            parkingrecordid = getRandomIntStringByLength(6);
        } else if (parkingrecordid.length() > 6) {
            parkingrecordid = parkingrecordid.substring(parkingrecordid.length() - 6, parkingrecordid.length());
        } else {
            parkingrecordid = genFixedString("0", 6 - parkingrecordid.length()) + parkingrecordid;
        }

        return ThreadSafeDateUtil.formatByConcisePattern(date) + parkingrecordid + getRandomIntStringByLength(4);
    }

    public static String genOutTradeNo(String prefix, Date date, String parkingrecordid, ThirdPayType thirdPayType) {
        if (prefix == null) {
            prefix = "";
        }
        return prefix + Integer.toHexString(thirdPayType.getPayCode()).toUpperCase() + genOutTradeNo(date, parkingrecordid);
    }

    /**
     * 生成指定字符串的固定长度的字符串
     *
     * @param s   要重复的字符串
     * @param len 返回的长度
     * @return
     */
    public static String genFixedString(String s, int len) {
        StringBuilder sb = new StringBuilder();
        do {
            sb.append(s);
        } while (sb.length() <= len);
        return sb.substring(0, len);
    }

    /**
     * 随机的包含字母数字的字符串
     *
     * @param len 长度
     * @return 随机字符串
     */
    public static String getRandomStringByLength(int len) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        SecureRandom random;
        try {
            random = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            random = new SecureRandom();
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 生成随机数字字符串
     *
     * @param len 长度
     * @return
     */
    public static String getRandomIntStringByLength(int len) {
        String base = "0123456789";
        SecureRandom random;
        try {
            random = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            random = new SecureRandom();
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getToken() {
        return UUID.randomUUID().toString().replace("-", "").toUpperCase();
    }

}
